package com.aji.controller;

import com.aji.dto.ElementAddDto;
import com.aji.dto.ElementChangeDto;
import com.aji.dto.ElementDto;
import com.aji.entity.Element;
import com.aji.result.PageResult;
import com.aji.result.Result;
import com.aji.service.ElementService;
import com.aji.vo.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.event.DocumentEvent;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/element")
@Api(tags = "微量元素数据管理")
@Slf4j
@CrossOrigin
public class ElementController {
    @Autowired
    ElementService elementService;

    @ApiOperation("训练 模型")
    @GetMapping("/trainModel/{modelIds}")
    private Result trainEle(@PathVariable List<Integer> modelIds) throws IOException, InterruptedException {
        elementService.trainEle(modelIds);
        return Result.success();
    }

    @ApiOperation("预测 根据id")
    @GetMapping("/predict/{modelIds}/{ids}")
    public ResponseEntity<List<ElementRunVo>> preEle(@PathVariable List<Integer> modelIds, @PathVariable List<Integer> ids) throws IOException, InterruptedException {
        ResponseEntity<List<ElementRunVo>> resultList = elementService.testEle(modelIds, ids);
        log.info(String.valueOf(resultList));
        // 返回过滤后的结果
        return resultList; // 这个已经是 JSON 格式
    }

    @ApiOperation("查询 微量元素记录，条件")
    @GetMapping
    private Result selectCon(ElementDto elementDto) {
        log.info("element模糊查询controller:{},{},{}", elementDto.toString());
        List<ElementVo> elementVoList = elementService.selectCon(elementDto);
        return Result.success(elementVoList);
    }

    @ApiOperation("查询 微量元素记录，分页，条件")
    @GetMapping("/{page}/{pageSize}")
    private Result<PageResult> selectPage(ElementDto elementDto, @PathVariable int page, @PathVariable int pageSize) {
        log.info("element模糊分页查询controller:{},{},{}", elementDto.toString(), page, pageSize);
        PageResult pageResult = elementService.pageQuery(elementDto, page, pageSize);
        List<ElementVo> records = pageResult.getRecords();
        List<ElementVoAdd> elementVoAddList = records.stream()
                .map(n -> {
                    ElementVoAdd elementVoAdd = new ElementVoAdd();
                    BeanUtils.copyProperties(n, elementVoAdd);
                    elementVoAdd.setElement(Arrays.asList(n.getElement().split("\\t")));
                    return elementVoAdd;
                })
                .collect(Collectors.toList());
        pageResult.setRecords(elementVoAddList);
        return Result.success(pageResult);
    }

    @ApiOperation("查询 微量元素记录,根据id,用于页面回显")
    @GetMapping("/{id}")
    private Result selectById(@PathVariable int id) {
        ElementByIdVoDdd elementByIdVoDdd = elementService.selectById(id);
        return Result.success(elementByIdVoDdd);
    }

    @ApiOperation("查询 微量元素记录,根据id,用于页面回显 new")
    @GetMapping("/new/{id}")
    private Result selectByIdNew(@PathVariable int id) {
        ElementByIdVo elementVo = elementService.selectByIdNew(id);
        return Result.success(elementVo);
    }

    @ApiOperation("删除 微量元素记录,批量")
    @DeleteMapping("/{ids}")
    private Result deleteByIds(@PathVariable List<Integer> ids) {
        log.info("element删除controller:{}", ids.toString());
        elementService.deleteByIds(ids);
        return Result.success();
    }

    @ApiOperation("增加 微量元素记录")
    @PostMapping
    private Result add(@RequestBody ElementAddDto elementAddDto) {
        log.info("element添加controller:{}", elementAddDto);
        elementService.add(elementAddDto);
        return Result.success();
    }

    @ApiOperation("批量增加 微量元素记录")
    @PostMapping("/batch")
    private Result addBatch(@RequestBody List<ElementAddDto> elementAddDtoList) {
        log.info("element添加controller:{}", elementAddDtoList);
        elementService.addBatch(elementAddDtoList);
        return Result.success();
    }


    @ApiOperation("读取 批量 微量元素记录")
    @PostMapping("/upload")
    public ResponseEntity<List<ElementBatchVo>> uploadFile(@RequestParam("file") MultipartFile file) {
        return elementService.uploadFile(file);
    }

    @ApiOperation("修改 微量元素记录")
    @PutMapping("/{id}")
    private Result upDate(@RequestBody ElementChangeDto elementChangeDto, @PathVariable int id) {
        log.info("element修改controller:{},{}", elementChangeDto, id);
        elementService.upDate(elementChangeDto, id);
        return Result.success();
    }
}
